package com.dxtan90;

import com.dxtan90.strategy.IFindStrategy;
import com.dxtan90.util.InterfaceScanner;
import com.dxtan90.util.PropertiesUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.URL;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

/**
 * @author shijunhao
 * @since 2024/4/16 11:33
 */
public class DoMain {
    private static final Logger logger = LoggerFactory.getLogger(DoMain.class);

    public static void main(String[] args) throws Exception {
        Map<String, IFindStrategy> strategyMap = new HashMap<>();
        String packages = PropertiesUtil.getProperty("strategy.packages");
        String[] packagesArray = packages.split(",");
        for (String findPackageName : packagesArray) {
            List<Class<?>> classesImplementingInterface = InterfaceScanner.findClassesImplementingInterface(findPackageName, IFindStrategy.class);
            for (Class<?> aClass : classesImplementingInterface) {
                IFindStrategy strategy = (IFindStrategy) aClass.newInstance();
                if (strategyMap.containsKey(strategy.getSourceUrl())) {
                    logger.warn("重复的实现策略,[{}]被替换为[{}]", strategyMap.get(strategy.getSourceUrl()).getClass().getName(), strategy.getClass().getName());
                }
                strategyMap.put(strategy.getSourceUrl(), strategy);
            }
        }
        logger.info("策略加载完成，加载策略[{}]个", strategyMap.size());
        Scanner scanner = new Scanner(System.in);
        String inputUrl = scanner.nextLine();
        URL url = new URL(inputUrl);
        IFindStrategy strategy = strategyMap.get(url.getProtocol() + "://" + url.getHost());
        if (strategy == null) {
            logger.error("未找到对应的实现策略：[{}]", url.getHost());
            return;
        }
        logger.info("找到对应的实现策略：[{}]", strategy.getClass().getName());
        strategy.getNovel(inputUrl);
    }

//    public static void main(String[] args) {
//        Gdbzkz qs = new Gdbzkz();
//        qs.getRecentUpdate("从手搓CPU开始横扫宇宙","/congshoucuocpukaishihengsaoyuzhou");
//    }

}
